package com.ajhlp.app.phoneDetailQuery.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.commons.io.output.FileWriterWithEncoding;
import org.apache.log4j.Logger;

import com.ajhlp.app.phoneDetailQuery.controller.QueryDetailController;
import com.ajhlp.app.phoneDetailQuery.model.ConfigurationNameEnum;
import com.ajhlp.app.phoneDetailQuery.model.PhoneNumDetail;


public class QueryPhoneDetailThread implements Runnable {
	
	private Logger log = Logger.getLogger(getClass());

	private IPhoneNumDetailService phoneNumDetailService = null;
	
	private List<String> mobile = new ArrayList<String>();
	
	private List<PhoneNumDetail> resultList = new Vector<PhoneNumDetail>();
	
	private String fileName;
	
	private int total_num;
	
	private int success_num;
	
	private int failure_num;
	
	private String percent = "0%";
	
	private boolean running = true;
	
	public QueryPhoneDetailThread(String fileName, List<String> mobile, IPhoneNumDetailService phoneNumDetailService) {
		this.phoneNumDetailService = phoneNumDetailService;
		this.mobile = mobile;
		this.fileName = fileName;
		this.total_num = mobile.size();
	}

	public void run() {
		// TODO Auto-generated method stub
		FileWriterWithEncoding fw = null;
		int queryTermTime = Integer.parseInt(IConfigModifyService.configProperties.get(ConfigurationNameEnum.QUERY_TERM_TIME.getName()));
		try {
			
			this.running = true;
			
			for(int i=0;i<mobile.size();i++){
				
				PhoneNumDetail detail = phoneNumDetailService.getDetail(mobile.get(i));
				if(detail == null){
					resultList.add(new PhoneNumDetail(mobile.get(i), "", ""));
					this.failure_num++;
				}else{
					resultList.add(detail);
					this.success_num++;
				}
				
				double count = success_num+failure_num;
				
				double per = count/total_num*100;
				
				percent = Math.round(per)+"%";
				
				if(i < mobile.size()-1){
				
					try {
						Thread.sleep(queryTermTime*1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
			File saveFile = new File(IConfigModifyService.configProperties.get(ConfigurationNameEnum.SAVE_PATH.getName())+this.fileName);
			
			saveFile.getParentFile().mkdirs();
			
			fw = new FileWriterWithEncoding(saveFile, "UTF-8");
			
			for(int i=0;i<resultList.size();i++){
				fw.write(resultList.get(i).toString()+"\r\n");
			}
			fw.flush();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("查询失败", e);
			
		} finally {
			this.running = false;
			QueryDetailController.getTasks().remove(fileName);
		}
	}

	public List<String> getMobile() {
		return mobile;
	}

	public String getFileName() {
		return fileName;
	}

	public int getTotal_num() {
		return total_num;
	}

	public int getSuccess_num() {
		return success_num;
	}

	public int getFailure_num() {
		return failure_num;
	}

	public boolean isRunning() {
		return running;
	}

	public String getPercent() {
		
		return percent;
	}

}
